<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Translate {   

        private $languages;
        private $fields = array('post_title','content');
        private $local_lang;

        public function __construct($params=NULL)
        { 
            // Get languages from config  
            foreach(config_item('wp_languages') as $key=>$value):         
                $this->languages[] =  $key;
                endforeach;  

            if(isset($params))
                $this->local_lang = $params['lang'];
        }

        private function get_string_between($string, $start, $end){
            $string = " ".$string;
            $ini = strpos($string,$start);
            if ($ini == 0) return "";
            $ini += strlen($start);
            $len = strpos($string,$end,$ini) - $ini;
            return substr($string,$ini,$len);
        }


        function getArray($post_title,$retrive_result=FALSE){

            $titles = array();

            foreach($this->languages as $l){

                $str = $this->get_string_between($post_title,'<!--:'.$l.'-->','<!--:-->');

                //echo $str."<br>";

                if($str!=""){
                    $titles[$l] = $str;
                }

            }

            if(count($titles)==0)return "No matches";

            if($retrive_result){
                if(isset($titles[$this->getLang()])){
                    return $titles[$this->getLang()];   //String
                } else return "";                       //String

            }else return $titles;                       //Array

        }

        function find_lang_field($post_title,$lang){

            $titles = array();

            foreach($this->languages as $l){

                $str = $this->get_string_between($post_title,'<!--:'.$l.'-->','<!--:-->');

                if($str!=""){
                    $titles[$l] = $str;
                }

            }

            if(count($titles)==0)return "";


            if(isset($titles[$lang])){
                return $titles[$lang];
            } else return "";

        }

        function updatePost($post_title, $title){

            $myArray = $this->getArray($post_title);

            if($myArray=='No matches') $myArray = array();
            $myArray[$this->getLang()] = $title;

            $string = "";
            foreach($this->languages as $l){
                if(isset($myArray[$l])) $string.= '<!--:'.$l.'-->'.$myArray[$l].'<!--:-->';
            }

            return $string;

        }

        function setLang($l){
            $this->local_lang = $l;
        }

        function getLang(){
            return $this->local_lang;
        }

        public function trans_res_array($res){

            foreach($res as $key=>$value){

                //Translate
                foreach($this->fields as $f){
                    $res[$key][$f] = $this->getArray($value[$f], TRUE);
                    if($res[$key][$f]=='')$res[$key][$f]='-Please translate-';                    

                }  

            }

            return $res;

        }

        public function trans_row_array($res){

            foreach($this->fields as $f):

                $res[$f] = $this->getArray($res[$f], TRUE);
                if($res[$f]=='')$res[$f]='-Please translate-';

                endforeach;

            return $res; 

        }

        public function trans_words_array($res,$asoc=NULL){

            $flds = array('word'); 

            //Associative array key=>value pair
            $words = array();

            foreach($res as $key=>$value){

                //Translate
                foreach($flds as $f){
                    $res[$key][$f] = $this->getArray($value[$f], TRUE);
                    if($res[$key][$f]=='')$res[$key][$f]='-Please translate-';

                    //Fill ADDOCIATIVE ARRAY
                    $words[$value['key']] = $res[$key][$f];                    

                }  

            }

            if(isset($asoc))
            {
                return $words; 
            }
            else return $res;


        }

        // create one language line for given string
        public function create_field($lang,$str)
        {
            $string = '<!--:'.$lang.'-->'.$str.'<!--:-->';
            return $string; 
        }

        // retrive one language line for given string
        public function find_field($lang,$str)
        {
            return $this->get_string_between($str,'<!--:'.$lang.'-->','<!--:-->');
        }
}         